##    duplicate-deleter. A duplicate file finder that deletes as long as the files are in the same directory
##    Copyright (C) 2013  Doktoro Reichard
##
##    This program is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import filecmp


path = None
shallow = False # check (http://docs.python.org/3/library/filecmp.html#filecmp.cmp)
                # True  - files are compared according to stats (inode, user id, group id, size, modification time; ...)
                # False - files are compared against content
                # True should compare the files well in about 90%, use False whenever you intentionally altered the file stats or want to compare the contents.

path = input("Path name: ")


for dirpath, dirnames, filenames in os.walk(path):                      # main walk
    for files in filenames:                                             # run all files
        orig_file = os.path.join(dirpath,files)                         # original file to compare
        for dirpath_2, dirnames_2, filenames_2 in os.walk(dirpath):     # second walk. note the dirpath instead of path
                                                                        # that way we shouldn't have to check previous directories
            for files_2 in filenames_2:
                test_file = os.path.join(dirpath_2,files_2)             # file to be tested
                if test_file != orig_file:                              # we can't compare the same file
                    try:
                        if filecmp.cmp(orig_file, test_file, shallow):               
                            os.remove(test_file)
                            print(test_file + " was deleted")
                    except FileNotFoundError:                       # needed because os.walk keeps a log of the files at startup
                        pass                                        # you can't compare what isn't there

            break    # since os.walk starts at dirpath it is redundant to keep exploring the rest of the tree.
